/**
 * @file NewMode.cpp
 *
 * @date 22/12/2011
 * @author Tales 
 *
 */

#include "NewMode.hpp"
#include <sstream>
#include "../controller.hpp"

namespace yep
{

NewMode::NewMode()
{

}

NewMode::~NewMode()
{
}

static const int STATE_SELECT = 0, STATE_WIDTH = 1, STATE_HEIGHT = 2, STATE_CONFIRM = 3, STATE_OK = 4, STATE_END = -1;
static const int MAX_WIDTH = 3000, MAX_HEIGHT = 3000;

pair<string, ConsoleModeBase::Function> NewMode::getNextLine(Editor & editor)
{
	Function function;
	string text;
	switch (state)
	{
	case STATE_SELECT:
		text = "Deseja criar uma nova imagem? (S/N): ";
		function = ConsoleModeBase::INPUT_YESNO;
		break;
	case STATE_WIDTH:
		text = "Digite a largura: ";
		function = ConsoleModeBase::INPUT_INT;
		break;
	case STATE_HEIGHT:
		text = "Digite a altura: ";
		function = ConsoleModeBase::INPUT_INT;
		break;
	case STATE_CONFIRM:
		text = "Ao criar uma nova imagem você perde todos os dados anteriores, deseja continuar? (S/N): ";
		function = ConsoleModeBase::INPUT_YESNO;
		break;
	case STATE_OK:
		text = "Imagem criada com sucesso... ";
		function = ConsoleModeBase::PAUSE;
		state = STATE_END;
		break;
	case STATE_END:
		text = "";
		function = ConsoleModeBase::END;
		break;
	default:
		break;
	}
	return make_pair(text, function);
}

void NewMode::handleAnswer(Editor & editor, string answer)
{
	switch (state)
	{
	case STATE_SELECT:
		if (answer == "yes")
			state = STATE_WIDTH;
		else
			state = STATE_END;
		break;
	case STATE_WIDTH:
	{
		stringstream s(answer);
		s >> width;
		if (width < MAX_WIDTH)
			state = STATE_HEIGHT;
		break;
	}
	case STATE_HEIGHT:
	{
		stringstream s(answer);
		s >> height;
		if (height < MAX_HEIGHT)
			state = STATE_CONFIRM;
		break;
	}
	case STATE_CONFIRM:
		if (answer == "yes")
		{
			editor.newPicure(make_pair(width, height));
			state = STATE_OK;
		}
		else
			state = STATE_SELECT;
		break;
	default:
		break;
	}
}

void NewMode::setup()
{
	ConsoleModeBase::setup();
	state = 0;
}

} /* namespace yep */
